package com.warrant.module.rpa.finance.controller;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eos.foundation.data.DataObjectUtil;
import com.warrant.module.rpa.finance.response.Result;
import com.warrant.module.rpa.finance.response.ResultGenerator;
import com.warrant.module.rpa.finance.service.BillService;
import com.warrant.module.rpa.finance.util.AESUtil;

import commonj.sdo.DataObject;

/**
 * 收款机器人api
 * 
 * @author cn
 * 
 */
@Path("/rest/services/collection")
public class CollectionController {

	private String secretKey = "14deb0035fc14ee9";

	/**
	 * 新增到账流水
	 * 
	 * @param content
	 *            报文文本
	 * @param appId
	 *            业务ID
	 * @return
	 */
	@POST
	@Path("/receiveBill")
	@Consumes(MediaType.TEXT_PLAIN)
	@Produces(MediaType.TEXT_PLAIN)
	public String receiveBill(String content, @QueryParam("appId") String appId) {
		String encryptStr = "";
		Result<?> result;
		try {
			// 1 | 解密报文 |
			String decryptStr = new String(AESUtil.decrypt(content, secretKey.getBytes("utf-8")), "utf-8");

			// 2 | 处理报文 |
			JSONObject responseJson = JSONObject.parseObject(decryptStr);
			System.out.println("解密后信息" + responseJson);
			String serialId = responseJson.getString("serialId");
			JSONArray data = responseJson.getJSONArray("data");

			// 3 | 更新业务信息 |
			BillService.callLogicComponentByInsertBill(convert(data), "1");

			result = ResultGenerator.genSuccessResult();
		} catch (Throwable e) {
			e.printStackTrace();
			result = ResultGenerator.genFailResult("错误:" + e.getMessage());
		}
		// 5 | 加密信息 |
		encryptStr = JSONObject.toJSONString(result);

		// 6 | 返回结果 |
		return AESUtil.encryptStr(encryptStr, secretKey);
	}

	/**
	 * 回调
	 * 
	 * @param content
	 *            报文文本
	 * @param appId
	 *            业务ID
	 * @return
	 */
	@POST
	@Path("/callbackBill")
	@Consumes(MediaType.TEXT_PLAIN)
	@Produces(MediaType.TEXT_PLAIN)
	public String callbackBill(String content, @QueryParam("appId") String appId) {
		String encryptStr = "";
		Result<?> result;
		try {
			// 1 | 解密报文 |
			String decryptStr = new String(AESUtil.decrypt(content, secretKey.getBytes("utf-8")), "utf-8");

			// 2 | 处理报文 |
			JSONObject responseJson = JSONObject.parseObject(decryptStr);
			System.out.println("解密后信息" + responseJson);
			String serialId = responseJson.getString("serialId");
			JSONArray data = responseJson.getJSONArray("data");

			// 3 | 更新业务信息 |
			BillService.callLogicComponentByInsertBill(convert(data), "1");

			// 4 | 組裝返回结果 |
			result = ResultGenerator.genSuccessResult();
		} catch (Throwable e) {
			e.printStackTrace();
			result = ResultGenerator.genFailResult("错误:" + e.getMessage());
		}
		// 5 | 加密信息 |
		encryptStr = JSONObject.toJSONString(result);

		// 6 | 返回结果 |
		return AESUtil.encryptStr(encryptStr, secretKey);
	}

	/**
	 * 转换对象
	 * 
	 * @param data
	 * @return
	 */
	private DataObject[] convert(JSONArray data) {
		List<DataObject> dataObjects = new ArrayList<DataObject>();
		for (int i = 0; i < data.size(); i++) {
			JSONObject jsonObject = data.getJSONObject(i);
			DataObject dataObject = DataObjectUtil.createDataObject("com.warrant.module.rpa.finance.data.BgCollectionBill");
			dataObject.set("serialnumber", jsonObject.get("tradeNo"));
			dataObject.set("chargeaccount", jsonObject.get("inBackacct"));
			// dataObject.set("companyname", jsonObject.get("fromClientName"));
			// dataObject.set("projectname", jsonObject.get("fromClientName"));
			dataObject.set("bankactname", jsonObject.get("fromClientName"));
			dataObject.set("bankaccount", jsonObject.get("tradeBankacct"));
			dataObject.set("bankname", jsonObject.get("tradeBankname"));
			dataObject.set("money", jsonObject.get("tradeMoney"));
			dataObject.set("intime", jsonObject.get("tradeTime"));
			dataObjects.add(dataObject);
		}
		return dataObjects.toArray(new DataObject[dataObjects.size()]);
	}

}
